package com.sujiewon.salesadmin.common.interceptor;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if(!this.checkSession(request)){
			ModelAndView mv = new ModelAndView();
			mv.setViewName("result/forwardSignin");
			throw new ModelAndViewDefiningException(mv);
		}
		return true;
	}
	
	@SuppressWarnings("unchecked")
	private boolean checkSession(HttpServletRequest request){
		boolean result = true;
		HttpSession session = request.getSession(true);
		HashMap<String, Object> userInfo = (HashMap<String, Object>) session.getAttribute("USER_INFO");
		if(userInfo == null || userInfo.isEmpty()){
			result = false;
		}
		return result;
	}

}
